package com.future.myjavatools.core.zhoupu;

import com.google.common.collect.Sets;

import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;

import java.util.ArrayList;
import java.util.Set;

/**
 * @program: brm
 * @description: diff工具类
 * @author: lianweiq
 * @create: 2019-11-01 09:30
 */
public class DiffUtils {
    private static Set<String> excelusion = Sets.newHashSet("id","updatedAt","createdAt","createdBy","updatedBy");

    public  static String buildDiffContext(Object origin, Object compare){


        DiffNode diff = ObjectDifferBuilder.buildDefault().compare(origin, compare);

        StringBuilder builder = new StringBuilder();
        diff.visit((node, visit) -> {
            final Object baseValue = node.canonicalGet(origin);
            final Object workingValue = node.canonicalGet(compare);
            if ("/".equals(node.getPath().toString())){
                return;
            }
            if (excelusion.contains(node.getPath().toString())){{
                return;
            }}
            if (baseValue == null && workingValue == null){
                return;
            }
            final String message = "{"+node.getPath().toString().replace("/","") + ":" +
                    baseValue + "->" + workingValue+"}";
            builder.append(message).append(",");
        });
        return builder.toString();
    }

    public static void main(String[] args){
        ArrayList<String> list = new ArrayList<>();
        list.add("test");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("test");

        System.out.println(buildDiffContext(list,list2));
    }
}
